<?php
namespace app\index\controller;

use think\Db;

class Person {
	public function index() {
		$user_id = input('get.user_id');
        $tab_type = input('get.tab_type');
        if ($tab_type == null) {
            $tab_type = 'text';
        }

        $user_data = $this->getUser($user_id);
        $texts_data = $this->getText($user_id);
        $replys_data = $this->getReply($user_id);

		return view('/index/person', [
			'user' => $user_data,
            'texts' => $texts_data,
            'replys' => $replys_data,
            'tab_type' => $tab_type
		]);
	}

    private function getUser($user_id) {
        if ($user_id != null) {
            $cuser = model('User');
            $user_data = $cuser::where('id', $user_id)->find();
            if ($user_data != null) {
                return $user_data;
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }

    private function getText($user_id) {
        if ($user_id != null) {
            $ctext = model('Text');
            $texts_data = $ctext::where('uid', $user_id)->order('create_time desc')->paginate(8, false, [           
                'type' => 'bootstrap',       
                'query' => ['user_id' => $user_id, 'tab_type' => 'text'],
                'var_page' => 'text_page'  
            ]);
            if ($texts_data != null) {
                return $texts_data;
            }
            else {
                return null;
            }            
        }
        else {
            return null;
        }
    }

    private function getReply($user_id) {
        if ($user_id != null) {
            $creply = model('Reply');
            $replys_data = $creply::where('uid', $user_id)->order('create_time desc')->paginate(8, false, [           
                'type' => 'bootstrap',       
                'query' => ['user_id' => $user_id, 'tab_type' => 'reply'],
                'var_page' => 'reply_page'  
            ]);
            
            if ($replys_data != null) {
                return $replys_data;
            }
            else {
                return null;
            }           
        }
        else {
            return null;
        }
    }

    public function setTopText() {
        $text_id = input('post.text_id');
        $set_top = input('post.set_top');
        if ($text_id != null and $set_top != null) {
            $ctext = model('Text');
            if ($set_top == 'true') {
                $ctext->save([
                    'if_top' => 1
                ],['id' => $text_id]);
            }
            else {
                $ctext->save([
                    'if_top' => 0
                ],['id' => $text_id]);
            }
            return json([
                'status' => 'success',
                'message' => '设置成功！',
                'data' => null
            ]);
        }
        else {
            return json([
                'status' => 'error',
                'message' => '设置错误，请重试！',
                'data' => null
            ]);
        }
    }

    public function modifyText() {
        dump('收到');
    }

    public function deleteText() {
        $text_id = input('post.content_id');
        $user_id = input('session.user_id');
        if ($text_id != null) {
            $ctext = model('Text');
            $cuser = model('User');
            $text_data = $ctext::where('id', $text_id)->find();
            if ($text_data != null) {
                $content = $text_data->content;
                foreach ($text_data->replys as $reply) {
                    $this->deleteOneReply($reply->id);
                }
                //$text_data->replys()->delete();
                $text_data->delete();
                $t = $this->deletePicture($content);
                $this->setMessage($text_id, 1);
                $cuser::where('id', $user_id)->setDec('heat', 10);
                $result = json([
                    'status' => 'success',
                    'message' => '删除成功！',
                    'data' => '/index/person?tab_type=text&user_id='.$user_id
                ]);
                return $result;
            }
            else {
                return json([
                    'status' => 'error',
                    'message' => '未找到内容！',
                    'data' => null
                ]);
            }
        }
        else {
            return json([
                'status' => 'error',
                'message' => '删除错误，请重试！',
                'data' => null
            ]);
        }
    }

    public function deleteReply() {
        $reply_id = input('post.content_id');
        $user_id = input('session.user_id');
        if ($reply_id != null) {
            $result = $this->deleteOneReply($reply_id);
            if($result) {
                return json([
                    'status' => 'success',
                    'message' => '删除成功！',
                    'data' => '/index/person?tab_type=reply&user_id='.$user_id
                ]);
            }
            else {
                return json([
                    'status' => 'error',
                    'message' => '未找到内容！',
                    'data' => null
                ]);
            }
        }
        else {
            return json([
                'status' => 'error',
                'message' => '删除错误，请重试！',
                'data' => null
            ]);
        }
    }

    private function deleteOneReply($reply_id) {
        $creply = model('Reply');
        $reply_data = $creply::where('id', $reply_id)->find();
        $text_id = $reply_data->tid;
        if ($reply_data != null) {
            $reply_data->delete();
            $this->updateHeat($text_id, -1, 5);
            $this->updateTextReplyNum($text_id);
            $this->setMessage($reply_id, 2);
            $this->setMessage($reply_id, 3);
            return true;
        }
        else {
            return false;
        }
    }

    private function deletePicture($content) {
        $start_sign = "src=";
        $end_sign = "\"";
        $picture_count = substr_count($content, $start_sign);
        $picture_list = array();
        $content_ = $content;
        $start = 0;
        $end = 0;

        for ($i = 0; $i < $picture_count; $i++) {
            $start = strpos($content, $start_sign, $end);
            $path_start = $start + 5;
            $path_end = strpos($content, $end_sign, $path_start);
            $path_length = $path_end - $path_start;
            $path = substr($content, $path_start, $path_length);
            array_push($picture_list, $path);
        }
        $num = 0;
        foreach ($picture_list as $path) {
            $picture_path = ROOT_PATH . substr($path, 3);
            if (file_exists($picture_path)) {
                unlink($picture_path);
                $num++;
            }            
        }

        if ($num == $picture_count) {
            return true;
        }
        else {
            return false;
        }
    }
    private function setMessage($sender, $type) {
        $cmsglist = model('Msglist');
        $data_list = $cmsglist::where('sender', $sender)->where('type', $type)->select();
        if ($data_list) {
            $update_list = array();
            foreach ($data_list as $data) {
                $update = array();
                $update['id'] = $data->id;
                $update['if_read'] = 1;
                array_push($update_list, $update);
            }
            $cmsglist->isUpdate()->saveAll($update_list);
            return true;
        }
        else {
            return false;
        }
    }

    private function updateTextReplyNum($text_id) {
        $creply = model('Reply');
        $reply_num = $creply::where('tid', $text_id)->count();
        $ctext = model('Text');
        $ctext->isUpdate(true)->save(['id' => $text_id, 'reply_num' => $reply_num]);
    }

    private function updateHeat($text_id, $type, $step) {
        $ctext = model('Text');
        $cuser = model('User');
        $text_data = $ctext::where('id', $text_id)->find();
        $user_id = $text_data->uid;
        if ($type == 1) {
            $cuser::where('id', $user_id)->setInc('heat', $step);
        }
        else {
            $cuser::where('id', $user_id)->setDec('heat', $step);
        }
    }
}